/*
 * @Author: wangdazhuang
 * @Date: 2025-02-20 19:13:43
 * @LastEditTime: 2025-02-20 20:22:57
 * @LastEditors: wangdazhuang
 * @Description: 
 * @FilePath: /pornhub_app/lib/views/player/views/download_ad.dart
 */

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:pornhub_app/assets/styles.dart';
import 'package:pornhub_app/components/circle_image.dart';
import 'package:pornhub_app/components/easy_button.dart';
import 'package:pornhub_app/model/advertisements/ad_resp_model.dart';
import 'package:pornhub_app/utils/ad_jump.dart';
import 'package:pornhub_app/utils/color.dart';
import 'package:pornhub_app/utils/enum.dart';
import 'package:pornhub_app/utils/extension.dart';
import 'package:pornhub_app/utils/initAdvertisementInfo.dart';

class DownloadAd extends StatefulWidget {
  const DownloadAd({super.key});

  @override
  State<StatefulWidget> createState() => _State();
}

class _State extends State<DownloadAd> {
  AdvertisementInfos? ad;

  @override
  void initState() {
    final list = initSequenceAdvertisementInfo(AdPlaceNameEnum.VIDEO_PAUSED);
    if (list.isNotEmpty) {
      ad = list.first;
      if (mounted) {
        setState(() {});
      }
    }
    super.initState();
  }

  void tapAd() {
    if (ad == null) return;
    jumpExternalAddress(ad!.adJump ?? '', null);
  }

  _buildDownloadBtn() {
    return EasyButton.child(
      Text(
        '下载体验',
        style: kTextStyle(Colors.black, fontsize: 13.w),
      ),
      backgroundColor: COLOR.hexColor("#ff9000"),
      borderRadius: BorderRadius.circular(16.w),
      width: 72.w,
      height: 32.w,
    );
  }

  _buildCenterContent() {
    return Expanded(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            ad?.adName ?? '',
            style: kTextStyle(Colors.white,
                fontsize: 14.w, weight: FontWeight.w500),
          ),
          Text(
            ad?.desc ?? '',
            style:
                kTextStyle(Colors.white.withValues(alpha: 0.6), fontsize: 12.w),
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    if (ad == null) return const SizedBox.shrink();
    return SizedBox(
      height: 50.w,
      child: Row(
        children: [
          CircleImage.network(ad!.adImage ?? '', size: 44.w),
          5.w.horizontalSpace,
          _buildCenterContent(),
          _buildDownloadBtn(),
        ],
      ),
    ).onTap(tapAd);
  }
}
